package com.foo.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.foo.servicesCall.MovieServiceStub.MovieData;
import com.foo.servicesCall.MovieServiceStub.MovieList;
import com.foo.servicesCall.MovieWSClient;
import com.foo.servicesCall.RestaurantWSClient;

/**
 * Servlet implementation class MovieServlet
 */
public class MoviesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MoviesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		try
		{
		HttpSession session = request.getSession(true);
		String sortBased = request.getParameter("SortBased");
		String paramName = "";
		MovieList movies = null;
		MovieWSClient client = new MovieWSClient();
		//Movies out = client.getAllMovieData();
		if(sortBased.equalsIgnoreCase("movieName"))
		{
			paramName = request.getParameter("movieName");
			MovieData res = null;
			res = client.getMovieInfoByName(paramName);
			movies = new MovieList();
			movies.addMovieList(res);
		}
		else if(sortBased.equalsIgnoreCase("genre"))
		{
			paramName = request.getParameter("genre");
			
			movies = client.getMovieInfoByGenre(paramName);
		}
		else if(sortBased.equalsIgnoreCase("theatreName"))
		{
			paramName = request.getParameter("theatreName");
						
			movies = client.getMovieInfoByTheatre(paramName);
		}
		else if(sortBased.equalsIgnoreCase("showTime"))
		{
			paramName = request.getParameter("showTime");
						
			movies = client.getMovieInfoByTime(paramName);
		}
		else if(sortBased.equalsIgnoreCase("movie_Rating"))
		{
			paramName = request.getParameter("movie_Rating");
						
			movies = client.getMovieInfoByType(paramName);
		}
		
	      session.setAttribute("MovieData", movies.getMovieList());
	      
	      RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/movies.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	      
	}
}

